﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;

namespace SortWithTemplate
{
    /// <summary>
    /// Class for array of PersonTemplate
    /// </summary>
    public class PersonsTemplate
    {
        #region Constructors

        /// <summary>
        /// Empty constructor
        /// </summary>
        public PersonsTemplate()
            : this(null)
        {
        }

        /// <summary>
        /// Full constructor
        /// </summary>
        /// <param name="personlist">List of persons</param>
        public PersonsTemplate(Collection<PersonTemplate> personList)
        {
            PersonList = new Collection<PersonTemplate>();
            PersonList = personList;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Sorting Persons
        /// </summary>
        /// <param name="Compare">The name of comparing delegate</param>
        public void Sort(string compareType)
        {
            PersonTemplate temp = new PersonTemplate();
            for (int i = 1; i < PersonList.Count; i++)
                for (int j = PersonList.Count - 1; j >= i; j--)
                    if (PersonList[j].CompareTemplate(PersonList[j - 1], compareType))
                    {
                        temp = PersonList[j - 1];
                        PersonList[j - 1] = PersonList[j];
                        PersonList[j] = temp;
                    }
        }

        /// <summary>
        /// Compare collections of persons
        /// </summary>
        /// <param name="comp">Incoming collection of persons</param>
        /// <returns>Result of aquals of two collection of persons</returns>
        public bool Compare(Collection<PersonTemplate> comp)
        {
            if (comp == null)
                throw new ArgumentNullException("comp in Compare method is null");

            if (comp.Count != PersonList.Count)
                return false;

            for (int i = 0; i < PersonList.Count; i++)
            {
                if (comp[i].Age != PersonList[i].Age)
                    return false;
                if (comp[i].Code != PersonList[i].Code)
                    return false;
                if (comp[i].FirstName != PersonList[i].FirstName)
                    return false;
                if (comp[i].LastName != PersonList[i].LastName)
                    return false;
            }

            return true;
        }

        #endregion

        #region Properties

        private Collection<PersonTemplate> PersonList { get; set; }

        #endregion
    }
}
